package com.alan.drools.demo.service;

import com.alan.drools.demo.model.Person;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.kie.api.KieBase;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Slf4j
public class DroolsService {

    public static KieBase rulekieBase() {
        KieHelper helper = new KieHelper();
        KieBase kieBase = null;
        try {
            List<String> droolsRuleList = listRules();
            for (String droolsRule : droolsRuleList) {
                helper.addContent(droolsRule, ResourceType.DRL);
            }
            kieBase = helper.build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return kieBase;
    }

    //将业务规则写到规则库中
    private static List<String> listRules() {
        List<String> listRules = new ArrayList<>();
        //模拟从数据库中取规则
        StringBuffer ruleDrl = new StringBuffer();
        ruleDrl.append("package rules \n ");
        ruleDrl.append("import   com.alan.drools.demo.model.Person; \n");
        ruleDrl.append(" rule    \'person_1\' \n");
        ruleDrl.append(" lock-on-active true \n");//让其不重复触发
        ruleDrl.append(" salience  10 \n");
        ruleDrl.append(" when \n");
        ruleDrl.append("  $p:Person(name==\'张三\',age==\'22\' )\n ");
        ruleDrl.append(" then \n");
        ruleDrl.append("  modify($p){ setName(\'张小三\' )} \n");
        ruleDrl.append("end \n");
        listRules.add(ruleDrl.toString());

        //模拟从数据库中取规则
        StringBuffer ruleDrl2 = new StringBuffer();
        ruleDrl2.append("package rules \n ");
        ruleDrl2.append("import   com.alan.drools.demo.model.Person; \n");
        ruleDrl2.append(" rule    \'person_2\' \n");
        ruleDrl2.append(" lock-on-active true \n");
        ruleDrl2.append(" salience  10 \n");
        ruleDrl2.append(" when \n");
        ruleDrl2.append("  $p:Person(age==\'30\' )\n ");
        ruleDrl2.append(" then \n");
        ruleDrl2.append("  modify($p){ setName(\'张小小三\' )} \n");
        ruleDrl2.append("end \n");
        listRules.add(ruleDrl2.toString());

        return listRules;
    }

    @Test
    public void testRule(){
        StringBuffer ruleDrl = new StringBuffer();
        ruleDrl.append("package rules \n ");
        ruleDrl.append("import   com.alan.drools.demo.model.Person; \n");
        ruleDrl.append(" rule    \'person_1\' \n");
        ruleDrl.append(" lock-on-active true \n");
        ruleDrl.append(" salience  10 \n");
        ruleDrl.append(" when \n");
        ruleDrl.append("  $p:Person(sex==\'1\')\n ");
        ruleDrl.append(" then \n");
        ruleDrl.append("  modify($p){ setName(\'张小三\' )} \n");
        ruleDrl.append("end \n");

        StringBuffer ruleDrl2 = new StringBuffer();
        ruleDrl2.append("package rules \n ");
        ruleDrl2.append("import   com.alan.drools.demo.model.Person; \n");
        ruleDrl2.append(" rule    \'person_2\' \n");
        ruleDrl2.append(" lock-on-active true \n");
        ruleDrl2.append(" salience  10 \n");
        ruleDrl2.append(" when \n");
        ruleDrl2.append("  $p:Person(age==\'30\' )\n ");
        ruleDrl2.append(" then \n");
        ruleDrl2.append("  modify($p){ setName(\'张小小三\' )} \n");
        ruleDrl2.append("end \n");

        KieHelper helper = new KieHelper();

        //按规则的加载顺序执行
        helper.addContent(ruleDrl.toString(), ResourceType.DRL);
        helper.addContent(ruleDrl2.toString(), ResourceType.DRL);
        KieSession ksession = helper.build().newKieSession();
        Person person = new Person();
        person.setSex("1");
        person.setAge("30");
        person.setName("张一");
        ksession.insert(person);
        int i = ksession.fireAllRules();
        ksession.dispose();
        System.out.println("i=" + i);
        System.out.println("person:"+ JSON.toJSONString(person));
    }

    @Test
    public void testRule2(){

        KieBase kieBase = DroolsService.rulekieBase();
        List<Person> list= PersonService.getlistPerson();
        for(Person person:list){
            KieSession kieSession= kieBase.newKieSession();
            kieSession.insert(person);
            int i=kieSession.fireAllRules();
            kieSession.dispose();
            log.info("i = {}, person:{}", i, JSON.toJSONString(person));
        }
    }

}
